package cn.nbt.busManage.service.impl;

import cn.nbt.busManage.dao.mapper.DriverMapper;
import cn.nbt.busManage.dao.mapper.ScheduleMapper;
import cn.nbt.busManage.model.dto.ScheduleDto;
import cn.nbt.busManage.model.entity.Driver;
import cn.nbt.busManage.model.entity.Schedule;
import cn.nbt.busManage.dao.repository.DriverRepository;
import cn.nbt.busManage.dao.repository.ScheduleRepository;
import cn.nbt.busManage.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author lyq
 * @time 2024/6/19 18:33
 */

@Transactional
@Service
public class IScheduleService implements ScheduleService {

    @Autowired
    private ScheduleRepository scheduleRepository;
    private ScheduleMapper scheduleMapper;
    private DriverMapper driverMapper;
    private DriverRepository driverRepository;

    /**
     * 查询所有排班信息
     * @return 排班信息
     */
    @Override
    public List<ScheduleDto> findAll() {
        Iterable<Schedule> all = scheduleRepository.findAll();
        ArrayList<ScheduleDto> scheduleDtos = new ArrayList<>();
        for(Schedule schedule : all){
            scheduleDtos.add(new ScheduleDto(schedule));
        }
        return scheduleDtos;
    }

    @Transactional("mybatisTransactionManager")
    @Override
    public List<ScheduleDto> findByName(String query) {
        Driver driver = driverMapper.findByDName(query);
        List<ScheduleDto> scheduleDtos = new ArrayList<>();
        if (driver != null) {
            List<Schedule> schedules = scheduleMapper.findByDriverId(driver.getId());
            for (Schedule schedule : schedules) {
                scheduleDtos.add(new ScheduleDto(schedule));
            }
            return scheduleDtos;
        }
        return null;
    }

    @Transactional("mybatisTransactionManager")
    @Override
    public List<ScheduleDto> findByDriverId(Integer driverId) {
        List<Schedule> byDriverId = scheduleMapper.findByDriverId(driverId);
        List<ScheduleDto> scheduleDtos = new ArrayList<>();
        for (Schedule schedule : byDriverId) {
            scheduleDtos.add(new ScheduleDto(schedule));
        }
        return scheduleDtos;
    }

    @Transactional("mybatisTransactionManager")
    @Override
    public void save(Integer driverId, Schedule schedule) {
        Schedule schedule1=new Schedule();
        schedule1.setId(schedule.getId());
        schedule1.setStartTime(schedule.getStartTime());
        schedule1.setEndTime(schedule.getEndTime());
        scheduleMapper.save(driverId,schedule1);
    }

    @Transactional("mybatisTransactionManager")
    @Override
    public void update(Integer driverId, Schedule nschedule) {
        Driver driver = (Driver) driverMapper.findByDriverId(driverId);
        if (driver == null) throw new RuntimeException("司机不存在");
        Schedule schedule = new Schedule();
        schedule.setId(schedule.getId());
        schedule.setStartTime(schedule.getStartTime());
        schedule.setEndTime(schedule.getEndTime());
        scheduleMapper.save(driverId, schedule);
    }

    @Transactional("mybatisTransactionManager")
    @Override
    public void deleteDriverSchedule(Integer driverId) {
        if (driverMapper.findByDriverId(driverId) == null) {
            throw new RuntimeException("司机不存在");
        }
        scheduleMapper.deleteDriverSchedule(driverId);
    }

    @Override
    public ScheduleDto findByScheduleId(Integer id) {
        Optional<Schedule> byId = scheduleRepository.findById(id);
        return byId.map(ScheduleDto::new).orElse(null);
    }
}
